package com.jic.member.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MemberType;
import com.jic.member.request.MemberTypeRequest;
import com.jic.member.response.MemberTypeResponse;
import com.jic.member.service.MemberTypeService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author: MJ
 * @date: 2020-01-09
 * @remark: 积分规则后台管理系统的服务
 */
@Slf4j
@RestController
@RequestMapping("/memberType")
public class MemberTypeController  {

    @Autowired
    MemberTypeService memberTypeService;


    /*
    * 分页查询
    * */
    @ApiOperation(value = "获取所有会员类别", notes="获取所有会员类别")
    @ResponseBody
    @RequestMapping("/queryList")
    public RestResult<PageResult<MemberTypeResponse>> queryList(@RequestBody MemberTypeRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MemberTypeRequest memberTypeRequest = BeanUtil.copy(form, MemberTypeRequest.class);
        PageResult<MemberType> result = memberTypeService.queryPage(memberTypeRequest, page);
        List<MemberTypeResponse> memberTypeResponseList = result.getRows().stream().map(temp -> {
            MemberTypeResponse memberTypeResponse = BeanUtil.copy(temp, MemberTypeResponse.class);
            return memberTypeResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MemberTypeResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberTypeResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value = "获取所有会员类别", notes="获取所有会员类别")
    @ResponseBody
    @RequestMapping("/queryAll")
    public RestResult<List<MemberType>> queryAll(@RequestBody MemberTypeRequest form) {
        MemberType memberType=new MemberType();
        memberType.setDeleteFlag(0);
        memberType.setDeptId(form.getAdminOperate().getDeptId());
        List<MemberType> result = memberTypeService.query(memberType);
        return RestResult.success(result);
    }

    /**
     * 新增/编辑会员类型
     * @param form
     * @return
     */
    @ApiOperation(value="新增/编辑会员类型", notes="新增/编辑会员类型")
    @ResponseBody
    @RequestMapping("/editMemberTypeInfo")
    public RestResult editMemberTypeInfo(@RequestBody MemberTypeRequest form) {
        RestResult restResult = new RestResult<>();
        try {
            MemberTypeRequest memberTypeRequest = BeanUtil.copy(form, MemberTypeRequest.class);
            //修改
            if(null!=memberTypeRequest.getId()&&!"".equals(memberTypeRequest.getId())){
                long flag=memberTypeService.update(memberTypeRequest);
                restResult=RestResult.success(restResult);
            }else {
                MemberType memberType = memberTypeService.getMemberTypeInfo(memberTypeRequest);
                if(null!=memberType){
                    restResult.setCode("9999");
                    restResult.setMsg("分组名称能重复");
                    return restResult;
                }
                long flag=memberTypeService.insert(memberTypeRequest);
                restResult=RestResult.success(restResult);
            }

        } catch (Exception e) {
            log.error("error",e);
            restResult=RestResult.error("9999",e.getLocalizedMessage());
        }
        return restResult;
    }
}
